package com.springboot.controller;

import com.springboot.constant.CommonConstant;
import com.springboot.domain.*;
import com.springboot.dto.EnterpriseModifyDTO;
import com.springboot.enums.ResultEnum;
import com.springboot.jpa.BankJPA;
import com.springboot.repository.*;
import com.springboot.service.EnterpriseService;
import com.springboot.tools.ResultUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2017/7/11.
 */
@RequestMapping(value = "/bank")
@RestController
public class EnterpriseController {

    private EnterpriseService enterpriseService;
    private EnterpriseRepository enterpriseRepository;
    private DemandSubRepository demandSubRepository;
    private DemandRepository demandRepository;
    private UserRepository userRepository;
    private BankJPA bankJPA;

    @Autowired
    public EnterpriseController(EnterpriseService enterpriseService, EnterpriseRepository enterpriseRepository, DemandSubRepository demandSubRepository,
                                DemandRepository demandRepository, UserRepository userRepository, BankJPA bankJPA) {
        this.enterpriseService = enterpriseService;
        this.enterpriseRepository = enterpriseRepository;
        this.demandSubRepository = demandSubRepository;
        this.demandRepository = demandRepository;
        this.userRepository = userRepository;
        this.bankJPA = bankJPA;
    }

    //完善修改企业信息
    @ResponseBody
    @PostMapping(value = "/enterprise/modifyEnterprise")
    public Result modifyEnterprise(@Valid @RequestBody EnterpriseModifyDTO enterpriseModifyDTO, BindingResult bindingResult, HttpSession session) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        enterpriseService.updateEnterpriseById(enterpriseModifyDTO, session);
        return ResultUtil.success("success");
    }

    @ResponseBody
    @GetMapping(value = "/demand/entersigned/list")
    public Result signedDemandList(@ApiParam Pageable pageable, HttpSession session) {
        String userId = (String) session.getAttribute("user");
        if (null == userId) {
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        Page<BtDemand> pages = demandRepository.findSignedListByEnter(userId, pageable);
        return ResultUtil.success(pages);
    }

    //企业信息详情
    @ResponseBody
    @GetMapping(value = "/enterprise/enterpriseInfoDetail")
    public Result enterpriseDetail(HttpSession session) {
        String id = (String) session.getAttribute("user");
        if (null == id) {
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        return enterpriseService.findEnterDetails(id);
    }

    //企业信息详情（其他人查看）
    @ResponseBody
    @GetMapping(value = "/enterprise/findEnterpriseDetailById/{id}")
    public Result findEnterpriseDetailById(@PathVariable String id) {
        return enterpriseService.findEnterDetailById(id);
    }

    //企业需求列表
    @ResponseBody
    @GetMapping(value = "/enterprise/demandSubList")
    public Result demandSubList(@ApiParam Pageable pageable, HttpSession session) {
        String id = session.getAttribute("user").toString();
//        String id="test";
        Page<BtDemandSub> btDemandSub = demandSubRepository.findByBankId(id, pageable);
        return ResultUtil.success(btDemandSub);
    }

    @ResponseBody
    @GetMapping(value = "/enterprise/logout/list")
    public Result logoutList(@ApiParam Pageable pageable) {
        Page<BtEnterprise> pages = enterpriseRepository.findByStatus(CommonConstant.LOGOUTING, pageable);
        return ResultUtil.success(pages);
    }

    //企业模糊查询(用于任务匹配)
    @ResponseBody
    @GetMapping(value = "/enterprise/query")
    public Result findEnterprise(@RequestParam(value = "userName", defaultValue = "", required = false) String userName,
                                 @RequestParam(value = "name", defaultValue = "", required = false) String name,
                                 @RequestParam(value = "province", defaultValue = "", required = false) String province,
                                 @RequestParam(value = "industry", defaultValue = "", required = false) String industry) {
        List<BtEnterprise> enterprises = enterpriseRepository.findConcatEnterprise(userName, name, province, industry);
        return ResultUtil.success(enterprises);
    }

    /**
     * 企业项目列表
     *
     * @param pageable
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/enterprise/project")
    public Result projectList(@ApiParam Pageable pageable, HttpSession session) {
        String userId = (String) session.getAttribute("user");
        if (userId == null) {
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        BtUser user = userRepository.findByUserId(userId);
        if (!user.getUserType().equals(CommonConstant.USER_ENTERPRISE)) {
            return ResultUtil.error(ResultEnum.MATCHING_USER_TYPE_ERRPR);
        }
        Page<BtDemand> demands = demandRepository.projectList(userId, pageable);
        return ResultUtil.success(demands);
    }

    //企业通过审核的列表
    @ResponseBody
    @GetMapping(value = "/enterprise/enterpriseList")
    public Result enterpriseList(@ApiParam Pageable pageable) {
        return ResultUtil.success(bankJPA.getEnterPriseList(CommonConstant.PASS, pageable));
    }
}
